#include "widgetboxtreewidget.h"

#include "../qdesignermimedata.h"

#include "../../../libs/core/qbaseitemdelegate.h"
#include "../../../libs/shared/qhostfactory.h"
#include "../../../libs/core/stylehelper.h"

#include <QHeaderView>

#include <QItemDelegate>
#include <QPainter>
#include <QApplication>
#include <QPalette>
#include <QMouseEvent>


WidgetBoxTreeWidget::WidgetBoxTreeWidget(QWidget *parent) :
    QTreeWidget(parent)
{
    setFocusPolicy(Qt::NoFocus);
    setIndentation(0);
    setRootIsDecorated(false);
    setColumnCount(1);
    header()->hide();
    header()->setResizeMode(QHeaderView::Stretch);
    setTextElideMode(Qt::ElideMiddle);
    setVerticalScrollMode(ScrollPerPixel);
    setFrameStyle(QFrame::NoFrame);
    setAcceptDrops(true);
    setAlternatingRowColors(true);
    setIconSize(QSize(24,24));
    setItemDelegate(new QBaseItemDelegate);
    m_expandIcon=StyleHelper::drawIndicatorIcon(this->palette(),this->style());
    init_widgetbox();
}

WidgetBoxTreeWidget::~WidgetBoxTreeWidget()
{

}

void WidgetBoxTreeWidget::drawRow(QPainter *painter, const QStyleOptionViewItem &options, const QModelIndex &index) const
{
    QColor c;
    QStyleOptionViewItem opt=options;
    if(index.data(DarkRole).toBool())
    {
        c=opt.palette.color(QPalette::Dark);
    }
    else
    {
        c=QColor(BACKGROUND_COLOR);
    }


    painter->fillRect(options.rect,c);
    opt.palette.setColor(QPalette::AlternateBase,c.lighter(112));
    QTreeWidget::drawRow(painter,opt,index);

    QColor color=static_cast<QRgb>(QApplication::style()->styleHint(QStyle::SH_Table_GridLineColor));
    painter->setPen(color);
    painter->drawLine(opt.rect.x(),opt.rect.bottom(),opt.rect.right(),opt.rect.bottom());
}

void WidgetBoxTreeWidget::init_widgetbox()
{
    foreach(tagHostInfo *info,QHostFactory::get_host_info())
    {
        add_widget(info);
    }

    this->expandAll();
}

void WidgetBoxTreeWidget::add_widget(tagHostInfo *info)
{
    QString group=info->m_show_group;
    QString name=info->m_show_name;
    if(group=="" || name=="")
    {
        return;
    }
    QTreeWidgetItem *p=m_name_to_group.value(group);
    QTreeWidgetItem *item;
    if(p==NULL)
    {
        p=new QTreeWidgetItem(this);
        p->setText(0,group);
        p->setFlags(Qt::ItemIsEnabled);
        p->setIcon(0,m_expandIcon);
        m_name_to_group.insert(group,p);
        p->setData(0,DarkRole,true);
    }

    item=new QTreeWidgetItem(p);
    item->setText(0,name);
    item->setIcon(0,QIcon(info->m_show_icon));
    item->setFlags(Qt::ItemIsEnabled);
    m_info_to_item.insert(info,item);
    m_item_to_info.insert(item,info);
}

void WidgetBoxTreeWidget::mousePressEvent(QMouseEvent *event)
{

    QTreeWidgetItem *item=itemAt(event->pos());

    if(item)
    {
        if(m_name_to_group.values().contains(item))
        {
            if(event->pos().x()+header()->offset()<20)
            {
                item->setExpanded(!item->isExpanded());
            }
        }
        else if(m_item_to_info.keys().contains(item))
        {
            if(event->button()!=Qt::LeftButton)
            {
                return;
            }
            tagHostInfo *info=m_item_to_info.value(item);
            if(info!=NULL)
            {
                handleMousePress(info);
            }
        }
    }

    QTreeWidget::mousePressEvent(event);
}

void WidgetBoxTreeWidget::handleMousePress(tagHostInfo *info)
{
    QList<QDesignerDnDItemInterface*> item_list;
    item_list.append(new WidgetBoxDnDItem(info->m_name,QCursor::pos()));

    foreach(QDesignerDnDItemInterface *item,item_list)
    {
        if(item->host()==NULL)
        {
            return;
        }
    }

    QDesignerMimeData::execDrag(item_list,this);
}
